package com.mrjoew.weekend.entities;

import com.mrjoew.weekend.entities.po.SysAccountPO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class WeekendUserDetails extends SysAccountPO implements UserDetails {

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 是否可用
     */
    private boolean enabled;

    private List<? extends GrantedAuthority> authorities;

    private Integer[] roleLevels;

    private Integer maxRoleLevel;

    /**
     * 用户的账号是否过期,过期的账号无法通过授权验证. true 账号未过期
     */
    private boolean accountNonExpired = true;

    /**
     * 用户的账户是否被锁定,被锁定的账户无法通过授权验证. true 账号未锁定
     */
    private boolean accountNonLocked = true;

    /**
     * 用户的凭据(pasword) 是否过期,过期的凭据不能通过验证. true 没有过期,false 已过期
     */
    private boolean credentialsNonExpired = true;

    public WeekendUserDetails(String username, String password, boolean enabled) {
        this.username = username;
        this.password = password;
        this.enabled = enabled;
    }

    public WeekendUserDetails(SysAccountPO sysAccountPO, boolean enabled) {

        this.enabled = enabled;
    }

    public WeekendUserDetails(String username, String password,
                              boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked,
                              List<GrantedAuthority> authorities) {
        this.username = username;
        this.password = password;
        this.enabled = enabled;
        this.accountNonExpired = accountNonExpired;
        this.credentialsNonExpired = credentialsNonExpired;
        this.accountNonLocked = accountNonLocked;
        this.authorities = authorities;
    }

    /**
     * 用户所拥有的权限
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }


    public void setAuthorities(List<? extends GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    /**
     * 用户拥有的权限等级
     */
    public Integer[] getRoleLevels() {
        return roleLevels;
    }

    public void setRoleLevels(Integer[] roleLevels) {
        this.roleLevels = roleLevels;
    }

    /**
     * 用户最大权限等级
     */
    public Integer getMaxRoleLevel() {
        return maxRoleLevel;
    }

    public void setMaxRoleLevel(Integer maxRoleLevel) {
        this.maxRoleLevel = maxRoleLevel;
    }

}
